package com.example.administrator.newxlmedical;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.administrator.baseproject.BaseActivity;

import java.util.ArrayList;

import adapters.ComboInfoAdapter;
import beans.ComboInfoBean;
import beans.WarningInfoBean;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import dialogs.GoodsDetialsDialog;
import interfaces.MyItemClickListener;
import netframe.GetRetrofit;
import netframe.NetAddress;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import utils.ActivityUtil;
import utils.CardinalNumberSortUtils;
import utils.ComboInfoSortUtils;
import utils.ComboSetSortUtils;
import utils.ViewUtil;

public class ComboInfoActivity extends BaseActivity {

    @BindView(R.id.iv_combo_info_home)
    ImageView ivHome;
    @BindView(R.id.iv_combo_info_return)
    ImageView ivReturn;
    @BindView(R.id.rl_combo_info_title)
    RelativeLayout rlTitle;
    @BindView(R.id.et_combo_info_combo_name)
    EditText etComboName;
    @BindView(R.id.tv_combo_info_goods)
    TextView tvGoods;
    @BindView(R.id.iv_combo_info_goods_sort)
    ImageView ivGoodsSort;
    @BindView(R.id.rl_combo_info_goods_sort)
    RelativeLayout rlGoodsSort;
    @BindView(R.id.tv_combo_info_specification)
    TextView tvSpecification;
    @BindView(R.id.iv_combo_info_specification_sort)
    ImageView ivSpecificationSort;
    @BindView(R.id.rl_combo_info_specification_sort)
    RelativeLayout rlSpecificationSort;
    @BindView(R.id.tv_combo_info_supplier)
    TextView tvSupplier;
    @BindView(R.id.iv_combo_info_supplier_sort)
    ImageView ivSupplierSort;
    @BindView(R.id.rl_combo_info_supplier_sort)
    RelativeLayout rlSupplierSort;
    @BindView(R.id.rv_combo_info_activity)
    RecyclerView recyclerview;
    @BindView(R.id.fl_combo_info_empty)
    FrameLayout flEmpty;
    private int comboID;
    private ArrayList<ComboInfoBean.DataBean> comboInfolist = new ArrayList<>();
    private ArrayList<ComboInfoBean.DataBean> sortList = new ArrayList<>();
    private ComboInfoAdapter adapter;
    private boolean isSortGoods, isSortSpecification, isSortSupplier;
    private int sortGoods, sortSpecification, sortSupplier;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_combo_info);
        ButterKnife.bind(this);
    }

    @Override
    public void initView() {
        super.initView();

        Intent intent = getIntent();
        String comboName = intent.getStringExtra("comboName");
        comboID = intent.getIntExtra("id", 0);
        etComboName.setText(comboName);

        LinearLayoutManager manager = new LinearLayoutManager(this);
        recyclerview.setLayoutManager(manager);
        adapter = new ComboInfoAdapter(this, comboInfolist);
        recyclerview.setAdapter(adapter);
        getComboInfoData(comboID);
    }

    @Override
    protected void setListener() {
        super.setListener();

        adapter.setItemClickListener(new MyItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                ComboInfoBean.DataBean bean = comboInfolist.get(position);

                String name = bean.getGoodName();
                String specification = "规格  :  " + bean.getSpecification();
                String supplier = "供应商  :  " + bean.getManufacturer();
                String count = "单位  :  " + bean.getGoodsUnit();
                String operator = "基数  :  " + bean.getCardinalNumber();

                String[] content = {name, specification, supplier, count, operator, "", "", ""};
                GoodsDetialsDialog dialog = new GoodsDetialsDialog(ComboInfoActivity.this, R.style.dialog, content);

                dialog.show();
            }
        });
    }

    /**
     * 获取套餐中的物品
     *
     * @param comboId 套餐ID
     */

    private void getComboInfoData(int comboId) {
        showLoadingView(flEmpty);
        GetRetrofit.getInstance().getComboInfoData(NetAddress.token, NetAddress.machineCode, String.valueOf(comboId))
                .enqueue(new Callback<ComboInfoBean>() {
                    @Override
                    public void onResponse(Call<ComboInfoBean> call, Response<ComboInfoBean> response) {
                        ComboInfoBean body = response.body();
                        if (body != null && body.getData() != null && body.getData().size() != 0) {
                            comboInfolist.addAll(body.getData());
                            sortList.addAll(body.getData());
                            ComboInfoSortUtils.sortComboInfoSGoods(comboInfolist, true);
                            adapter.notifyDataSetChanged();
                        } else {
                            showEmptyView(flEmpty);
                        }
                        hideLoadingView();
                    }

                    @Override
                    public void onFailure(Call<ComboInfoBean> call, Throwable t) {
                        hideLoadingView();
                        showEmptyView(flEmpty);
                    }
                });

    }

    @OnClick({R.id.iv_combo_info_home, R.id.iv_combo_info_return, R.id.rl_combo_info_goods_sort, R.id.rl_combo_info_specification_sort, R.id.rl_combo_info_supplier_sort})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_combo_info_home:
                ActivityUtil.jumpToActivity(this,MainActivity.class);
                finish();
                break;
            case R.id.iv_combo_info_return:
                finish();
                break;
            case R.id.rl_combo_info_goods_sort:
                sort(1);
                break;
            case R.id.rl_combo_info_specification_sort:
                sort(3);
                break;
            case R.id.rl_combo_info_supplier_sort:
                sort(2);
                break;
        }
    }

    private void sort(int type) {
        if (type == 1) { // 按物品名排序
            if (sortGoods != 2) {
                isSortGoods = !isSortGoods;
                sortGoods++;
                ComboInfoSortUtils.sortComboInfoSGoods(comboInfolist, isSortGoods);
            } else {
                clearSortDate();
            }
        } else {
            sortGoods = 0;
            isSortGoods = false;
        }

        if (type == 2) { // 按供应商排序
            if (sortSupplier != 2) {
                sortSupplier++;
                isSortSupplier = !isSortSupplier;
                ComboInfoSortUtils.sortComboInfoSupplier(comboInfolist, isSortSupplier);
            } else {
                clearSortDate();
            }
        } else {
            sortSupplier = 0;
            isSortSupplier = false;
        }

        if (type == 3) {
            if (sortSpecification != 2) {
                sortSpecification++;
                isSortSpecification = !isSortSpecification;
                ComboInfoSortUtils.sortComboInfoSpecification(comboInfolist, isSortSpecification);
            } else {
                clearSortDate();
            }
        } else {
            sortSpecification = 0;
            isSortSpecification = false;
        }

        ViewUtil.setSortColor(this, tvGoods, ivGoodsSort, sortGoods);
        ViewUtil.setSortColor(this, tvSupplier, ivSupplierSort, sortSupplier);
        ViewUtil.setSortColor(this, tvSpecification, ivSpecificationSort, sortSpecification);

        adapter.notifyDataSetChanged();
    }

    private void clearSortDate() {
        comboInfolist.clear();
        comboInfolist.addAll(sortList);
        sortGoods = 0;
        sortSupplier = 0;
        sortSpecification = 0;
    }
}
